<template>
	<el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
		class="demo-ruleForm" :size="formSize" status-icon>
		<el-upload v-model:file-list="fileList" class="upload-demo"
			action="http://localhost:5099/api/Files/Upload/FormImg" :on-preview="handlePreview"
			:on-remove="handleRemove" list-type="picture"
			name="files"
			:on-success="successHandle">
			<el-button type="primary">上传产品图片</el-button>
		</el-upload>
		<el-form-item label="产品名称" prop="ProductName">
			<el-input v-model="ruleForm.ProductName" />
		</el-form-item>
		<el-form-item label="产品价格" prop="ProductPrice">
			<el-input v-model="ruleForm.ProductPrice" />
		</el-form-item>

		<el-form-item>
			<el-button type="primary" @click="submitForm(ruleFormRef)">
				保存
			</el-button>
			<el-button @click="resetForm(ruleFormRef)">重置</el-button>
		</el-form-item>
	</el-form>
</template>

<script lang="ts" setup>
	import { reactive, ref } from 'vue'
	import type { ComponentSize, FormInstance, FormRules,UploadProps, UploadUserFile } from 'element-plus'
	import { ElNotification } from 'element-plus'
	import productinfoapi from "../../api/productinfoapi"
	const emit = defineEmits(['onSave'])

	interface RuleForm {
		ProductName : string
		ProductPrice : string
		ProductPict:string
		
	}

	const formSize = ref<ComponentSize>('default')
	const ruleFormRef = ref<FormInstance>()
	const ruleForm = reactive<RuleForm>({
		ProductName: '',
		ProductPrice: '',
		ProductPict:'',
	})
const fileList = ref<UploadUserFile[]>([
  
])

const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}

const handlePreview: UploadProps['onPreview'] = (file) => {
  console.log(file)
}
	const validatePass = (rule : any, value : any, callback : any) => { }
	const validatePass2 = (rule : any, value : any, callback : any) => { }

	const rules = reactive<FormRules<RuleForm>>({
		ProductName: [
			{  message: '请输入产品名称', trigger: 'blur' },
			{ min: 2, max: 5, message: '2-5个字符', trigger: 'blur' },
		],
		ProductPrice: [
			{
				type: 'string',
				message: '请输入产品价格',
				trigger: 'blur',
			}
		],
	})
	
	const successHandle = (response: any, uploadFile: UploadFile, uploadFiles: UploadFiles)=>{
		console.log(response);
		ruleForm.ProductPict=response.data[0];
	}

	const submitForm = async (formEl : FormInstance | undefined) => {
		if (!formEl) return
		await formEl.validate((valid, fields) => {
			if (valid) {
				productinfoapi.post(ruleForm).then(res => {
					if (res.code == 1) {
						ElNotification({
							title: 'Success',
							message: '添加成功',
							type: 'success',
							duration: 800,
							onClose: () => {
								emit("onSave", true)
							}
						})
					} else {
						ElNotification({
							title: 'Error',
							message: '失败',
							type: 'Error',
							duration: 800,
						})
					}
				}).catch(err => {
					ElNotification({
						title: 'Error',
						message: '失败',
						type: 'Error',
						duration: 800,
					})
				})
			} else {
				console.log('error submit!', fields)
			}
		})
	}

	const resetForm = (formEl : FormInstance | undefined) => {
		if (!formEl) return
		formEl.resetFields()
	}
</script>